package com.jeomo.mem.provider.web;

import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.masterdata.starter.enums.UseScopeEnum;
import com.jeomo.mem.provider.request.MemStatusRequest;
import com.jeomo.mem.provider.request.MemberPointRuleDetail;
import com.jeomo.mem.provider.request.MemberPointRuleInfo;
import com.jeomo.mem.provider.request.MemberPointRuleRequest;
import com.jeomo.mem.provider.vo.MemberPointsRuleVo;
import com.jeomo.mem.starter.dto.MemberPointRuleDto;
import com.jeomo.mem.starter.entity.MemberPointsRule;
import com.jeomo.mem.starter.entity.MemberPointsRuleList;
import com.jeomo.mem.starter.service.IMemberPointsRuleService;
import com.jeomo.mem.starter.service.IMemberPointsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员积分规则
 * @author:Jackpot
 * @create 2020-09-09 16:24
 * @description:
 */
@RestController
@RequestMapping("/member/point")
public class MemberPointsRuleController {

    @Autowired
    private IMemberPointsService memberPointsService;

    @Resource
    private IMemberPointsRuleService memberPointsRuleService;

    @ApiOperation("会员积分规则定义接口")
    @PostMapping("/rule")
    public CommonResult saveRule(@RequestBody @ApiParam("会员积分规则") @Validated MemberPointRuleRequest memberPointRuleRequest){

        MemberPointRuleInfo memberPointRuleInfo = memberPointRuleRequest.getMemberPointsRule();
        List<MemberPointRuleDetail> memberPointRuleDetails = memberPointRuleRequest.getMemberPointsRuleList();

        MemberPointsRule memberPointsRule = BeanCopyUtil.copyProperties(memberPointRuleInfo,MemberPointsRule.class);
        List<MemberPointsRuleList> memberPointsRuleList =
                BeanCopyUtil.copyListProperties(memberPointRuleDetails,MemberPointsRuleList::new);

        int resultCode = memberPointsService.saveRule(memberPointsRule, memberPointsRuleList);

        if (resultCode >0){
            return CommonResult.success("保存成功");
        }else if (resultCode == -1){
            return CommonResult.failure("数据已发生变化,请刷新后重试！");
        }else if (resultCode == -2){
            return CommonResult.failure("数据已被删除，操作失败！");
        }else if (resultCode == -3){
            return CommonResult.failure("配置项不能重复！");
        }else{
            return CommonResult.failure("保存失败");
        }
    }

    @ApiOperation("获取一条积分规则的详情信息")
    @GetMapping("/rule/{ruleCode}")
    public CommonResult getPointRuleById(@PathVariable String ruleCode){
        MemberPointRuleDto memberPointRule = memberPointsService.getMemberPointRule(ruleCode);
        return CommonResult.success(memberPointRule);
    }

    @ApiOperation("分页获取会员积分规则")
    @PostMapping("/rules/page")
    public CommonResult<PageResponseVo<MemberPointsRuleVo>> getPointRulesByPage(@RequestBody PageRequest pageRequest){
        PageQueryDto dto = memberPointsRuleService.initParamAndSort(pageRequest);
        PageResponseDto pageResponseDto = memberPointsRuleService.pageSearch(dto);
        PageResponseVo<MemberPointsRuleVo> memPointsRulePageResVo = BeanCopyUtil.copyPageProperties(pageResponseDto, MemberPointsRuleVo.class);
        return CommonResult.success(memPointsRulePageResVo);
    }

    @ApiOperation("计算积分")
    @GetMapping("/point/{levelCode}/{goodsCode}/{amount}")
    public CommonResult getPoints(@PathVariable String levelCode,@PathVariable String goodsCode,@PathVariable double amount){
        int points = memberPointsService.getPoints(levelCode, goodsCode, amount, UseScopeEnum.GOODS);
        return CommonResult.success(points);
    }

    @ApiOperation("删除单条会员积分规则")
    @DeleteMapping("/rule/{ruleCode}")
    public CommonResult deletePointRule(@PathVariable String ruleCode){
        int i = memberPointsService.deleteRule(ruleCode);
        return i>0?CommonResult.success("删除成功"):CommonResult.failure("操作失败");
    }

    @ApiOperation("停用/启用会员积分规则")
    @PostMapping("/rule/status")
    public CommonResult updateRuleStatus(@RequestBody MemStatusRequest memStatusRequest){

        String ruleCode = memStatusRequest.getRuleCode();
        Boolean status = memStatusRequest.getStatus();
        int i = memberPointsRuleService.updateRuleStatus(ruleCode, status);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

}
